package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import excepcion.DAOExcepcion;

import negocio.*;
import modelo.*;

/**
 * Servlet implementation class IngresarDueno
 */
@WebServlet("/InsertarClienteServlet")
public class InsertarClienteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertarClienteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String nombre = request.getParameter("txtNombres");
		String paterno = request.getParameter("txtApePaterno");
		String materno = request.getParameter("txtApeMaterno");
		String sexo = request.getParameter("cboSexo");
		String tipoDocumento = request.getParameter("cboTipoDoc");
		String nroDocumento = request.getParameter("txtDocumento");
		String direccion = request.getParameter("txtDireccion");
		String distrito = request.getParameter("cboDistrito");
		String email = request.getParameter("txtEmail");
		String clave = request.getParameter("txtClave");
		String fecha = request.getParameter("txtFecNacimiento");
		String celular = request.getParameter("txtCelular");
		TipoPersona tipoPersona = new TipoPersona(2, "Cliente");
		
		PersonaNegocio negocio = new PersonaNegocio();
		
		try {
			negocio.insertarPersona(nombre, paterno+" "+materno, sexo, tipoDocumento, nroDocumento, direccion, distrito, email, clave , fecha, celular, tipoPersona);
			System.out.println("Se ingreso al cliente Correctamente");
			response.sendRedirect("index.jsp");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}		
	}
}
